package tuiLayer;
import java.util.Scanner;
import controlLayer.*;
public class LoanUI
{
    private LoanController loanCtr;
    Scanner keyboard = new Scanner(System.in);
    public LoanUI()
    {
    }
    
    public void loanMenu()
    {        
       loanCtr = new LoanController();
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
           String choise = subMenus();
           switch(choise)
           {
               case "1":
               System.out.print('\u000C');
               System.out.println(" (1) Create Loan");
               creatLoan();
               break;
               case "2":
               System.out.print('\u000C');
               System.out.println(" (2) Update Loan");   
               updateLoan();
               break;
               case "3":
               System.out.print('\u000C');
               System.out.println(" (3) Delete Loan");  
               deleteLoan();              
               break; 
               case "4":
               System.out.print('\u000C');
               System.out.println(" (4) Find Loan");
               findLoan();
               break;
               case "5": 
               exit = true;
               break;
           }
       }
    }
    public void findLoan()
    {
         System.out.println("enter person phonenumber");
         String phone = keyboard.next();
         loanCtr.findLoan(phone);
    }
    public void deleteLoan()
    {
        System.out.println("enter person phonenumber");
        String phone = keyboard.next();
        loanCtr.deleteLoan(phone);
    }
    
    public void creatLoan()
    {
           System.out.println("enter dvd title");
           String title = keyboard.next();  
           if(loanCtr.getdvd(title) != null)
           {
              System.out.println("Find person");
              System.out.println("enter phone");
              String phone = keyboard.next();
               if(loanCtr.getPerson(phone)!= null)
               {
                   System.out.println("angiv id");
                   int id = keyboard.nextInt();
                   System.out.println("set lone dages");
                   String borrowDate = keyboard.next();
                   System.out.println("enter periode");
                   String period = keyboard.next();
                   System.out.println("enter state");
                   String state = keyboard.next();
                   loanCtr.createLoan(id, borrowDate, period,state,loanCtr.getdvd(title),loanCtr.getPerson(phone));
                }
                else
               {
                   System.out.println("Person not found");
               }
        }        
    }
    public void updateLoan()
    {
      System.out.println("input phone");
      String phone = keyboard.next(); 
      if(loanCtr.getLoan(phone) != null)
      {
          System.out.println("Set new period");
          String period = keyboard.next();
          loanCtr.updateLoan(phone, period);          
      }
      else
      {
          System.out.println("Loan not found");
        }
    }
    
    
    
    public String subMenus()
    {      // makes an object keyboard to read input from the screen       
       System.out.print('\u000C');
       System.out.println("*** Loan Menu ***");
       System.out.println(" (1) Create Loan");
       System.out.println(" (2) Update Loan");
       System.out.println(" (3) Delete Loan");
       System.out.println(" (4) Find Loan");
       System.out.println(" (5) back");
       System.out.print("\n choose: ");
            
       String choise = keyboard.next();
       return choise;
    }
    
}
